
var productId = getQuery("productId")
window.onload = function (){
    if (productId != null && productId != ''){
        buy();
    }
    showShop();
}
async function  buy(){
    await axios.get("/custom/shop/buy",{
        params:{
            productId,
        }
    }).then(e=>{
        if (e.data == "ok"){
            showShop();
        }
    })
}

/**
 * 显示购物车信息
 */
function showShop(){
    axios.get("/custom/shop/findByUser",null).then(e=>{
        var info = e.data;
        var str ="";
        var total =0;
        for (var i=0;i<info.length;i++){
            var obj = info[i];
            str+=`<tr>
<td>${obj.productBean.name}</td>
<td>${obj.productBean.address}</td>
<td>${obj.productBean.price}</td>
<td><input type="number" value="${obj.num}" onblur="updateNum(${obj.id},this)"></td>
<td>${obj.productBean.price * obj.num}</td>
<td><a href="javascript:del(${obj.id})">移出商品</a></td>
</tr>`;
            total += obj.productBean.price * obj.num;
        }
    $("shopData").innerHTML=str;
    $("sumPrice").innerHTML =total;
    });
}


function del(carId){
    axios.get("/custom/shop/del",{
        params: {
            carId
        }
    }).then(e=>{
        if (e.data=="ok"){
            showShop();
        }
    })
}

/**
 * 修改商品购买数量
 */
function updateNum(carId,txtObj){
    axios.get("/custom/shop/update",{
        params:{
            carId,
            newNum:txtObj.value
        }
    }).then(e=>{
        if (e.data !="ok"){
            alert("购物车数量更新失败")
        }else{
            showShop();
        }

    })
}
function addOrder(){
    location.href="/custom/addOrder.html?sumPrice="+ $("sumPrice").innerHTML;
}